<template>
	<view class="contenter">
		<!-- #ifdef MP-WEIXIN -->
		<view v-if="isCanUse">
			<view>
				<view class='header'>
					<image src='../../static/login_bg.jpg'></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>第一步：获得你的公开信息(昵称，头像、地区等)</text>
				</view>
				<button class="bottom" hover-class="none" type='primary' @click="getUserProfile">
					登录授权
				</button>
			</view>
		</view>
		<view v-if="isCanUse2">
			<view>
				<view class='header'>
					<image :src='avatar'></image>
				</view>

				<view class='content'>
					<view>申请获取以下权限</view>
					<text>第二步：获得你的手机号用于绑定登录</text>
				</view>
				<button class='bottom' hover-class="none" type='primary' open-type="getPhoneNumber"
					@getphonenumber="PhoneNumber">
					授权手机登录
				</button>
			</view>
		</view>
		<!-- #endif -->
	</view>
</template>
<script>
	import {
		sendSmsCode
	} from '../../api/user/userApi.js'
	import context from '../../lib/java110/Java110Context.js';

	const constant = context.constant;
	// import {wechatSession,decodePhone,wechatLogin} from "@/api/user"
	export default {
		data() {
			return {
				code: "",
				rawData: '',
				encryptedData: '',
				iv: '',
				signature: '',

				SessionKey: '',
				session_key: '',
				openid: '',
				avatar: '',
				nick_name: "",
				avatarUrl: null,
				isCanUse: false, //默认为true
				isCanUse2: false, //默认为true

			}
		},
		methods: {
			getuserNew() {
				uni.login({
					provider: 'weixin',
					success: res => {
						uni.request({
							url: 'https://api.weixin.qq.com/sns/jscode2session',
							method: 'GET',
							data: {
								appid: 'wxab37b50bd1a6410a', //你的小程序的APPID  
								secret: '4ef52345329a5b9a01859e12a9a9a017', //你的小程序的secret,  
								js_code: res.code, //wx.login 登录成功后的code  
								grant_type: 'authorization_code',
							},
							success: (cts) => {
								// 换取成功后 暂存这些数据 留作后续操作  
								this.openId = cts.data.openid //openid 用户唯一标识  
								this.unionid = cts.data
									.unionid //unionid 开放平台唯一标识   当公众号和小程序同时登录过才会有
								this.session_key = cts.data.session_key //session_key  会话密钥  
								console.log(cts,"777777777777777777777")
								console.log(this.openId, this.session_key)
							}
						});
					}
				});
			},


			getUserProfile() {
				var that = this
				// #ifdef MP-WEIXIN
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								success: (res) => {
									that.code = res.code
									// that.wechatSession(res.code)
									console.log('获取信息code', res)
								}
							})
							uni.getUserProfile({
								desc: '登录',
								success: async (res) => {
									console.log("1112222", res)
									uni.setStorageSync('isCanUse', false)
									that.isCanUse = false
									that.isCanUse2 = true
									that.avatar = res.userInfo.avatarUrl
									that.nick_name = res.userInfo.nickName
									console.log(">>>>>>>>>>>>>>>>>", res.rawData)
									uni.setStorageSync('wxInfo', res.rawData);
									that.rawData = res.rawData
									that.encryptedData = res.encryptedData
									that.iv = res.iv
									that.signature = res.signature
									// uni.setStorageSync('user', res)
								},
								fail: (res) => {
									console.log(res)
								}
							})

						} else {
							uni.showToast({
								title: '请先安装微信或升级版本',
								icon: "none"
							})
						}
					}
				})
				//#endif
			},
			//登录
			login() {
				let _this = this;
				uni.showLoading({
					title: '登录中...'
				})
			},
			//解析手机号
			PhoneNumber(e) {
				let that = this
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					that.decodePhone(e)
				} else {
					console.log("用户点击了拒绝")
				}
			},
			//解密手机号码
			decodePhone(e) {
				console.log("用户信息", e)
				let that = this
				console.log("解析手机号", e, that.code)
				uni.showLoading({
					title: '请稍后...',
					mask: false
				})
				uni.request({
					url: constant.url.userLogin,
					method: 'POST',
					data: {
						code: that.code,
						encryptedData: that.encryptedData,
						iv: that.iv,
						session_key:that.session_key,
						// code:e.detail.code,
						rawData: that.rawData
					},

					success: (res) => {
						console.log("res授权拿到openid", res)
						if (res.data.code == 0) {
							uni.hideLoading()
							console.log("res数据", res)
							uni.showToast({
								title: '授权成功',
								duration: 1500
							})
							console.log("用户信息登录", res)
							uni.setStorageSync('login', res.data.login); //用户登录
							uni.setStorageSync('userInfo', res.data.userInfo); //用户信息
							uni.setStorageSync('token', res.data.token);
							uni.reLaunch({
								url: '/pages/index/index'
							})

						} else {
							uni.showToast({
								title: '网络异常',
								icon: 'none'
							})
						}
					}
				})
			}
		},
		onLoad() { //默认加载
			this.getuserNew();
			console.log(uni.getStorageSync('isCanUse'))
			if (uni.getStorageSync('token') != "") {
				uni.reLaunch({ //信息更新成功后跳转到小程序首页
					url: '/pages/index/index'
				})
			} else {
				this.isCanUse = true
			}
		}
	}
</script>

<style lang="scss" scoped>
	button[type=primary] {
		background: linear-gradient(90deg, #ff406d 17%, #ff7273 100%);
		color: #fff;
	}

	.header {
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 500rpx;
		margin: auto;
		line-height: 650rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
		border-radius: 50%;

	}

	.content {
		margin-top: 50rpx;
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 180rpx 110rpx;
		font-size: 35rpx;
	}

	.contenter {
		width: 100%;
		height: calc(100vh - 0rpx);
		background-color: #191b1f;
	}
</style>
